using System;
using sys = System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using System.Xml.Serialization;
using Common.Shared.Xml.Wddx;
using System.Xml;
using System.IO;
using Common.Shared.Xml.Serialization;
using Common.Shared.Net;

namespace Common.Shared.Tests {

	public class WddxTests {
		WddxSerializer serialzier;
		public WddxTests ( ) {
			serialzier = new WddxSerializer ( );
		}

		[Fact]
		public void SerializeTest ( ) {
			WddxPacket packet = new WddxPacket ( );
			packet.Data.Value = new WddxStructure ( );

			( packet.Data.Value as WddxStructure ).Variables.Add ( new WddxVariable ( "test", new WddxString ( "foooooy" ) ) );
			( packet.Data.Value as WddxStructure ).Variables.Add ( new WddxVariable ( "test2", new WddxNull ( ) ) );
			( packet.Data.Value as WddxStructure ).Variables.Add ( new WddxVariable ( "test3", new WddxNumber ( 4500 ) ) );

			WddxArray arr = new WddxArray ( );
			arr.Items.Add ( new WddxString ( "foobar" ) );
			arr.Items.Add ( new WddxString ( "foo" ) );
			arr.Items.Add ( new WddxString ( "bar" ) );
			arr.Items.Add ( new WddxNumber ( 437 ) );
			arr.Items.Add ( new WddxNull ( ) );
			arr.Items.Add ( new WddxBoolean ( true ) );
			arr.Items.Add ( new WddxBoolean ( false ) );
			arr.Items.Add ( new WddxDateTime ( ) );
			arr.Items.Add ( new WddxDateTime ( new DateTime ( 1970, 1, 1, 0, 0, 0 ) ) );
			arr.Items.Add ( new WddxString ( "f" ) );
			arr.Items.Add ( new WddxCharArray ( "this is a char array" ) );
			WddxStructure strct = new WddxStructure ( );
			strct.Variables.Add ( new WddxVariable ( "test", new WddxString ( "foooooy" ) ) );
			strct.Variables.Add ( new WddxVariable ( "test2", new WddxNull ( ) ) );
			strct.Variables.Add ( new WddxVariable ( "test3", new WddxNumber ( 4500 ) ) );

			arr.Items.Add ( strct );
			( packet.Data.Value as WddxStructure ).Variables.Add ( new WddxVariable ( "testA", arr ) );

			using ( FileStream fs = new FileStream ( "c:\\test-wddx.xml", FileMode.Create, FileAccess.Write ) ) {
				serialzier.Serialize ( fs, packet, true );
			}
		}

		[Fact]
		public void PacketFromObjectTest ( ) {
			List<object> data = new List<object> ( );

			ProxyInfo pi = new ProxyInfo ( );
			pi.ProxyPort = 8080;
			pi.ProxyServer = "proxy.ups.com";
			pi.Name = "ups";
			pi.Username = "trr4rac";
			pi.Password = "p@ssw0rd1";
			pi.BypassLocal = true;
			pi.BypassList.Add ( "ups.com" );
			pi.BypassList.Add ( "upsers.com" );
			pi.BypassList.Add ( "10.*" );
			pi.RequiresLogin = true;
			pi.OverrideDefaultProxy = false;
			pi.Enabled = true;

			data.Add ( pi );
			data.Add ( Encoding.Default.GetBytes ( "this is a string that I am converting to a byte array" ) );

			data.Add ( typeof ( Boolean ) );
			data.Add ( typeof ( Nullable<bool> ) );

			data.Add ( new WddxCharArray ( "testing adding a wddx data object to my list" ) );
			bool? nullableBool = null;
			data.Add ( nullableBool );

			data.Add ( new Uri ( "http://google.com" ) );

			WddxPacket packet = WddxPacket.FromObject ( data );
			using ( FileStream fs = new FileStream ( "c:\\wddx-object.xml", FileMode.Create, FileAccess.Write ) ) {
				serialzier.Serialize ( fs, packet, true );
			}
		}
		
		[Fact]
		public void DeserializeTest ( ) {
			using ( FileStream fs = new FileStream ( "c:\\wddx-object.xml", FileMode.Open, FileAccess.Read ) ) {
				WddxPacket packet = serialzier.Deserialize ( fs );
			}
		}
	}
}
